Page({
  data: {
    openid: "",
    loginstate: "0",
    openid: "",
    userEntity: null,
    terminal: "",
    osVersion: "",
    phoneNumber: "",
    showModal: false,
  },
  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        that.setData({ openid: res.data });
      },
      fail: function () {
        wx.login({
          success: (res) => {
            wx.setStorageSync('code', res.code)
            wx.request({
              url: 'http://127.0.0.1:8080/login',
              method: 'POST',
              data: {
                code: res.code
              },
              success(res) {
                console.log(res);
                wx.setStorageSync('openid', res.data.openid)
              }
            })
          },
        })
      }
    });
  },
  onGotUserInfo: function (e) {
    var that = this;
    if (e.detail.errMsg == "getUserInfo:ok") {
      console.log("getuserinfo success")
      that.showDialogBtn();
    }
  },
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  onShow: function (openid, userInfo, phoneNumber) {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/login',
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log("login success1")
      },
      fail(res) {
        console.log("fail1");
        console.log(res);
      }
    })
  },

  getPhoneNumber: function (e) {
    var that = this;
    console.log(e);
    var detail = e.detail;
    if (detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: 'http://localhost:8080/getPhoneNumber',
        method: "POST",
        header: {
          'content-type': 'application/json'
        },
        data: {
          code: detail.code,
        },
        success(e) {
          console.log(e)
          var data = e.data;
          wx.request({
            url: 'http://localhost:8080/mylogin',
            method: "POST",
            header: {
              'content-type': 'application/json'
            },
            data: {
              phone: data.phone_info.phoneNumber,
              openid: wx.getStorageSync('openid')
            },
            success(e) {
              wx.navigateTo({
                url: '/pages/home/home',
              })
            }
          })
        },
        fail: function () {
          console.log("fail!");
        }
      })
    }
    that.hideModal();
  },
})